<?php
namespace Admin\Controller;

use Think\Page;

class IndustryController extends AdminController {

    protected $_cache_tag = 'industry_cache';

    public function index() {
        $cpage = (int)$_GET['p'] > 0 ? (int)$_GET['p'] : 1;
        $where = [];
        $result = M('industry')->where($where)->order('sort ASC,id DESC')->page($cpage, $this->page_size)->select();

        $count = M('Industry')->where($where)->field('id')->count();
        $Page = new Page($count, $this->page_size);
        $this->assign([
            'result' => $result,
            'page' => $Page->show(),
        ]);
        $this->display();
    }

    public function add() {

        if (IS_POST) {
            $data = [];
            $data['name'] = I('name', '', 'addslashes,trim');
            $data['sort'] = I('sort', 255, 'intval');
            if ($_FILES['icon'] && $_FILES['icon']['error'] == 0) {
                $data['icon'] = $this->uploadImage($_FILES['icon']);
            }

            if (M('Industry')->data($data)->add() !== false) {
                return $this->success('行业添加成功');
            }

            $this->error('添加失败');
        } else {
            $this->display();
        }
    }

    public function edit() {

        if (IS_POST) {
            $data = [];
            $data['id'] = I('id', 0, 'intval');
            if (empty($data['id'])) {
                return $this->error('参数错误');
            }
            $data['name'] = I('name', '', 'addslashes,trim');
            $data['sort'] = I('sort', 255, 'intval');
            if ($_FILES['icon'] && $_FILES['icon']['error'] == 0) {
                $data['icon'] = $this->uploadImage($_FILES['icon']);
            } elseif (isset($data['icon'])) {
                unset($data['icon']);
            }
            if (M('Industry')->data($data)->save() !== false) {
                return $this->success('编辑成功');
            }
            $this->error('编辑失败');
        } else {
            $id = I('id', 0, 'intval');
            $result = M('Industry')->where(['id' => $id])->find();
            $this->assign('result', $result);
            $this->display();
        }
    }

    /**
     * 分类排序
     */
    public function sort() {
        $sorts = I('sort', 0, 'intval');
        foreach ($sorts as $key => $val) {
            M('Industry')->where(['id' => $key])->setField('sort', $val);
        }
        $this->success('操作成功');
    }

    public function delete() {
        $id = I('id', 0, 'intval');
        if (empty($id)) {
            return $this->error('参数错误');
        }
        $industry = M('Industry')->where(['id' => $id])->find();
        if (empty($industry)) {
            return $this->error('该分类不存在');
        }
        if (M('Industry')->delete($id) !== false) {
            return $this->success('删除成功');
        }
        return $this->success('删除失败');
    }

    public function cache($tag = false) {

        $cache = [];
        $industry = M('Industry')->order('sort ASC,id DESC')->select();
        foreach ($industry as $item) {
            $cache[$item['id']] = $item;
        }
        unset($industry);
        S($this->_cache_tag, $cache);
        if ($tag === true) {
            return $cache;
        }
        return $this->success('缓存更新成功');
    }

    public function clearCache() {
        S($this->_cache_tag, null);
    }

}